package com.ido.otaclient.two;

import android.content.Context;

import com.ido.otaclient.base.BasePreference;
import com.ido.otaclient.env.AppEnv;

/**
 * Copyright (c) 2019 深圳市爱都科技有限公司. All rights reserved.
 *
 * @Author: xyb
 * @CreateDate: 2020-03-06 10:23
 * @description
 */
public class TwoToOnePerfence extends BasePreference {

    private static final String NAME = "two_preference";

    private static final String TASK_NAME = "taskName";
    private static final String FIREWARE_NEW_NAME = "firewareNewName";
    private static final String FIREWARE_NEW_VERSION = "firewareNewVersion";
    private static final String FIREWARE_NEW_PATH = "firewareNewPath";
    private static final String BOOTLOADER_NAME = "bootloaderName";
    private static final String BOOTLOADER_VERSION = "bootloaderVersion";
    private static final String BOOTLOADER_PATH = "bootloaderPath";
    private static final String DEVICE_ID = "deviceId";
    private static final String PLATFORM_NAME = "platformName";
    /**
     * 升级配置
     */
    private TwoToOneConfig mTwoToOneConfig;

    private static volatile TwoToOnePerfence instance;

    private static Context getContext() {
        return AppEnv.instance().getContext();
    }

    public static TwoToOnePerfence getInstance() {
        if (instance == null) {
            synchronized (TwoToOnePerfence.class) {
                if (instance == null) {
                    instance = new TwoToOnePerfence();
                }
            }
        }
        return instance;
    }

    /**
     * 保存配置在本地
     * @param twoToOneConfig
     */
    public void saveTwoToOneConfig(TwoToOneConfig twoToOneConfig){
        if(twoToOneConfig == null){
            twoToOneConfig = new TwoToOneConfig();
        }
        saveString(getContext(),NAME,TASK_NAME,twoToOneConfig.getTaskName());
        saveString(getContext(),NAME,FIREWARE_NEW_NAME,twoToOneConfig.getFirewareNewName());
        saveString(getContext(),NAME,FIREWARE_NEW_VERSION,twoToOneConfig.getFirewareNewVersion());
        saveString(getContext(),NAME,BOOTLOADER_NAME,twoToOneConfig.getBootloaderName());
        saveString(getContext(),NAME,BOOTLOADER_VERSION,twoToOneConfig.getBootloaderVersion());
        saveString(getContext(),NAME,FIREWARE_NEW_PATH,twoToOneConfig.getFirewareNewPath());
        saveString(getContext(),NAME,BOOTLOADER_PATH,twoToOneConfig.getBootloaderPath());
        saveString(getContext(),NAME,DEVICE_ID,twoToOneConfig.getDeviceId());
        saveString(getContext(),NAME,PLATFORM_NAME,twoToOneConfig.getUploadPlatform());
        mTwoToOneConfig = twoToOneConfig;
    }

    /**
     * 从本地取出数据
     * @return
     */
    public TwoToOneConfig getTwoToOneConfig(){
        mTwoToOneConfig = new TwoToOneConfig();
        mTwoToOneConfig.setTaskName(getString(getContext(),NAME,TASK_NAME));
        mTwoToOneConfig.setFirewareNewName(getString(getContext(),NAME,FIREWARE_NEW_NAME));
        mTwoToOneConfig.setFirewareNewVersion(getString(getContext(),NAME,FIREWARE_NEW_VERSION));
        mTwoToOneConfig.setBootloaderName(getString(getContext(),NAME,BOOTLOADER_NAME));
        mTwoToOneConfig.setBootloaderVersion(getString(getContext(),NAME,BOOTLOADER_VERSION));
        mTwoToOneConfig.setFirewareNewPath(getString(getContext(),NAME,FIREWARE_NEW_PATH));
        mTwoToOneConfig.setBootloaderPath(getString(getContext(),NAME,BOOTLOADER_PATH));
        mTwoToOneConfig.setDeviceId(getString(getContext(),NAME,DEVICE_ID));
        mTwoToOneConfig.setUploadPlatform(getString(getContext(),NAME,PLATFORM_NAME));
        return mTwoToOneConfig;
    }
}
